# encoding: utf-8
from .. import db
from datetime import datetime

class Article(db.Model):
    __tablename__ = "articles"
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(40), nullable=False)
    content = db.Column(db.Text, nullable=False)
    author_id = db.Column(db.Integer, db.ForeignKey("users.id"))
    category_id = db.Column(db.Integer, db.ForeignKey("categories.id"))
    is_valide = db.Column(db.Boolean, default=True)
    created_time = db.Column(db.DateTime)

    def __init__(self, title, content, category_id, author_id):
        self.title = title
        self.category_id = category_id
        self.content = content
        self.author_id = author_id
        self.created_time = datetime.now()

    def __repr__(self):
        return "<Article {0}>".format(self.title)

    # 添加
    def add(self):
        db.session.add(self)
        db.session.commit()